<?php
import( 'de.ceus-media.framework.krypton.core.Model' );
/**
 *	Data Model of Users.
 *	@package		mv2.model
 *	@extends		Framework_Krypton_Core_Model
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			15.10.2006
 *	@version		0.2
 */
/**
 *	Data Model of Users.
 *	@package		mv2.model
 *	@extends		Framework_Krypton_Core_Model
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			15.10.2006
 *	@version		0.2
 */
class Model_User extends Framework_Krypton_Core_Model
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@param		int			$userId		ID of User
	 *	@return		void
	 */
	public function __construct( $userId = NULL )
	{
		$tablename	= 'users';
		$fields	= array(
			'userId',
			'creatorId',
			'status',
			'username',
			'email',
			'password',
			'notify',
			'language',
			'created',
			'modified',
			);
		$primary_key	= 'userId';
		$indices		= array(
			'status',
			'username',
			'email',
			'notify',
			);
		parent::__construct( $tablename, $fields, $primary_key, $$primary_key );
		$this->setIndices( $indices );
	}
	
	/**
	 *	Returns ID of User by its Name.
	 *	@access		public
	 *	@param		string		$username		Name of User
	 *	@return		int
	 */
	public function getUidFromUsername( $username )
	{
		$this->defocus();
		$this->focusIndex( 'username', $username );
		$data	= $this->get();
		$this->defocus();
		if( count( $data ) )
			return $data['userId'];
		return 0;
	}

	/**
	 *	Returns the Name of an existing User.
	 *	@access		public
	 *	@param		int			$userId		User ID
	 *	@return		string
	 */
	public function getUsernameFromUid( $userId )
	{
		$this->focusPrimary( $userId );
		$data	= $this->get( true );
		$this->defocus();
		if( count( $data ) )
			return $data['username'];
	}

	/**
	 *	Returns the Mail Address of an existing User.
	 *	@access		public
	 *	@param		int			$userId		User ID
	 *	@return		string
	 */
	public function getEmailFromUid( $userId )
	{
		$this->focusPrimary( $userId );
		$data	= $this->get( true );
		$this->defocus();
		if( count( $data ) )
			return $data['email'];
	}

	/**
	 *	Returns the Name of current User.
	 *	@access		public
	 *	@return		string
	 */
	public function getUsername()
	{
		if( $this->isFocused() )
		{
			$data	= $this->get( false, true);
			if( isset( $data['username'] ) )
				return $data['username'];
		}
		return '';
	}

	/**
	 *	Returns the Notify Status of current User.
	 *	@access		public
	 *	@return		bool
	 */
	public function isNotify()
	{
		if( $this->isFocused() )
		{
			$data	= $this->get( false, true);
			return $data['notify'];
		}
		return false;
	}
}
?>
